using B.S.XZYData.Api.Wrter.Application.Command.Role;
using B.S.XZYData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace B.S.XZYData.Api.Wrter.Controllers
{
    /// <summary>
    /// 角色相关写操作接口（增删改）
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class RoleController : ControllerBase
    {
        private readonly IMediator _mediator;
        public RoleController(IMediator mediator)
        {
            _mediator = mediator;
        }

        /// <summary>
        /// 新增角色及其权限
        /// </summary>
        /// <param name="command">角色信息及权限ID列表</param>
        /// <returns>操作结果，Data为受影响行数</returns>
        [HttpPost]
        [Authorize]
        public async Task<IActionResult> Add(AddRoleCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        /// <summary>
        /// 修改角色及其权限
        /// </summary>
        /// <param name="command">角色ID、角色名、权限ID列表</param>
        /// <returns>操作结果，Data为受影响行数</returns>
        [HttpPut]
        [Authorize]
        public async Task<IActionResult> Update(UpdateRoleCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        /// <summary>
        /// 删除角色及其权限关联
        /// </summary>
        /// <param name="command">角色ID</param>
        /// <returns>操作结果，Data为受影响行数</returns>
        [HttpDelete]
        [Authorize]
        public async Task<IActionResult> Delete([FromQuery]DeleteRoleCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }
        /// <summary>
        /// 修改角色状态
        /// </summary>
        /// <param name="request">命令</param>
        /// <returns>返回统一包装类</returns>
        [HttpPut]
        [Authorize]
        public async Task<ApiResult<int>> UpdateRoleState([FromQuery]UpdateRoleStateCommand request)
        {
            var result = await _mediator.Send(request);
            return result;
        }
    }
} 